import edu.princeton.cs.algs4.*;
import java.util.Iterator;
import java.util.NoSuchElementException;

public class LinkStack<Item> implements Iterable<Item>
{
    private int n;
    private Node first;

    private class Node
    {
        private Item item;
        private Node next;
    }

    public LinkStack()
    {
        n = 0;
        first = null;
        assert check();
    }

    public boolean isEmpty()
    {
        return first == null;
    }

    public int size()
    {
        return n;
    }

    public void push(Item item)
    {
        Node oldfirst = first;
        first = new Node();
        first.item = item;
        first.next = oldfirst;
        n++;
        assert check();
    }
    
    public Item pop()
    {
        if(isEmpty())
        {
            throw new NoSuchElementException("Stack Underflow");
        }
        Item item = first.item;
        first = first.next;
        n--;
        assert check();
        return item;
    }

    public Item peek()
    {
        if(isEmpty())
        {
            throw new NoSuchElementException("Stack Underflow");
        }
        return first.item;
    }

    public String toString()
    {
        StringBuilder s = new StringBuilder();
        for(Item item : this)
        {
            s.append(item + " ");
        }
        return s.toString();
    }

    public Iterator<Item> iterator()
    {
        return new LinkIterator();
    }

    private class LinkIterator implements Iterator<Item>
    {
        private Node current = first;
        public boolean hasNext()
        {
            return current != null;
        }
        public Item next()
        {
            if(!hasNext())
            {
                throw new NoSuchElementException("Stack UnderFlow");
            }
            Item item = current.item;
            current = current.next;
            return item;
        }
    }
    public boolean check()
    {
        if(n < 0)
        {
            return false;
        }
        if(n == 0)
        {
            if(first != null)
            {
                return false;
            }
        }
        else if(n == 1)
        {
            if(first == null)
            {
                return false;
            }
            if(first.next != null)
            {
                return false;
            }
        }
        else
        {
            if(first == null)
            {
                return false;
            }
            if(first.next == null)
            {
                return false;
            }
        }
        int numberOfNodes = 0;
        for(Node x = first; x != null && numberOfNodes < n; x=x.next)
        {
            numberOfNodes++;
        }
        if(numberOfNodes != n)
        {
            return false;
        }
        return true;
    }
    public static void main(String[] args)
    {
        LinkStack<String> stack = new LinkStack<String>();
        while(!StdIn.isEmpty())
        {
            String item = StdIn.readString();
            if(!item.equals("-"))
            {
                stack.push(item);
            }
            else if(!stack.isEmpty())
            {
                StdOut.print(stack.pop() + " ");
            }
        }
        StdOut.print("("+stack.size()+"left on stack)");
    }
}

